#include "FoodTable.h"

QSqlDatabase db1 = QSqlDatabase::database("KFC");    //获取指向数据库的连接

FoodTable::FoodTable(QObject *parent) : QObject(parent)
{

}

/**
  * food_table操作
  */

//数据库操作:创建食物表food_table
void FoodTable::createFoodTable(){
    QSqlQuery query(db1);

    /**
      * name:食物名字
      * price:价格
      * content:对食物的描述
      */
    const QString cmd = R"(
                        create table food_table(id integer primary key,
                                           name varchar(50),
                                           price integer,
                                           content varchar(1000),
                                           is_null integer);)";

    query.exec(cmd);    //执行建表语句

//    if(query.exec(cmd)){
//        qDebug()<<"init food_table success";
//    }else{
//        //打印sql语句错误信息
//        qDebug()<<"init food_table error"<<query.lastError();
//    }
}

//数据库操作:向food_table中插入默认数据(用来测试功能)
void FoodTable::insertDefaultFood(){
    QSqlQuery query(db1);

    const QString cmd_1 = "insert into food_table(name,price,content,is_null) values('原神套餐',648,'op最爱',0);";
    const QString cmd_2 = "insert into food_table(name,price,content,is_null) values('汉堡',20,'肥宅专属',0);";
    const QString cmd_3 = "insert into food_table(name,price,content,is_null) values('可乐',10,'快乐水嘿嘿',0);";

    query.exec(cmd_1);
    query.exec(cmd_2);
    query.exec(cmd_3);
}

//数据库操作:向food_table中插入食物
void FoodTable::insertFood(Food f){
    QSqlQuery query(db1);
    //数据插入(预分配空间)
    query.prepare("insert into food_table values(?,?,?,?,?);");
    //绑定要插入的数值
    query.bindValue(1,f.name);
    query.bindValue(2,f.price);
    query.bindValue(3,f.content);
    query.bindValue(4,f.is_null);
    //调用exec()函数在query中插入上述值
    query.exec();
}

//数据库操作:查询food_table中所有的未下架食物
Food* FoodTable::queryAllUnnullFood(){
    QSqlQuery query(db1);

    const QString cmd = "select * from food_table where is_null=0";
    query.exec(cmd);

    Food* foodList = new Food[100];
    int i = 0;


    while(query.next()){
        foodList[i].name = query.value(1).toString();
        foodList[i].price = query.value(2).toInt();
        foodList[i].content = query.value(3).toString();
        foodList[i].is_null = query.value(4).toInt();
//        qDebug()<<foodList[i].name<<" "<<foodList[i].price<<" ";
        i++;
    }

    return foodList;
}

//查询所有食物
Food* FoodTable::queryAllFood(){
    QSqlQuery query(db1);

    const QString cmd = "select * from food_table";
    query.exec(cmd);

    Food* foodList = new Food[100];
    int i = 0;


    while(query.next()){
        foodList[i].name = query.value(1).toString();
        foodList[i].price = query.value(2).toInt();
        foodList[i].content = query.value(3).toString();
        foodList[i].is_null = query.value(4).toInt();
//        qDebug()<<foodList[i].name<<" "<<foodList[i].price<<" ";
        i++;
    }

    return foodList;
}

//上架或者下架食物
void FoodTable::updateFood(QString name,int control){
    QSqlQuery query(db1);

    QString cmd = "";

    if(control == 1){
        //下架食物
        cmd = "update food_table set is_null = 1 where name = '" + name + "'";
    }else if(control == 0){
        //上架食物
        cmd = "update food_table set is_null = 0 where name = '" + name + "'";
    }

    query.exec(cmd);
}

//修改食物信息
void FoodTable::updateFoodInform(Food food,QString _name){
    QSqlQuery query(db1);

    QString name = food.name;
    int price = food.price;
    QString content = food.content;

    QString cmd = "update food_table set name = '" + name + "' where name = '" + _name + "'";
    query.exec(cmd);
    cmd = "update food_table set price = " + QString::number(price) + " where name = '" + _name + "'";
    query.exec(cmd);
    cmd = "update food_table set content = '" + content + "' where name = '" + _name + "'";
    query.exec(cmd);
}

//根据名字查询食物
Food FoodTable::queryFoodByName(QString name){
    QSqlQuery query(db1);

    QString cmd = "select * from food_table where name = '" + name + "'";
    query.exec(cmd);

    Food food;

    while(query.next()){
        food.name = query.value(1).toString();
        food.price = query.value(2).toInt();
        food.content = query.value(3).toString();
        food.is_null = query.value(4).toInt();
    }

    return food;
}
